<?php

$hack  = $_GET['hack'];
$operation = $_POST['operation'];
$id = $_POST['id'];
$name = sqlite_escape_string($_POST['title']);
$parent = $_POST['parent'];					
$note = sqlite_escape_string($_POST['note']);					
$search = sqlite_escape_string($_POST['search']);
$replace = $_POST['replace'];


if(empty($parent)) {
  $parent="NULL";
}
if(empty($id)) {
  $id="0";
}

$db=new SQLiteDatabase("folioge.db", 0666, $error);

if ($operation == "create_node") {
  $query = "INSERT INTO folioge(parent,  name) VALUES(".$parent.",'".$name."')";
  $row=$result=$db->query($query);
  $id = $db->lastInsertRowid();
  echo $id;
} elseif ($operation == "remove_node") {
  crawl($id,$db);
  $t = explode(",", $s);
  for ($i=0; $i<count($t)-1; $i++) {
    $result = $db->query("DELETE FROM folioge where id=".$t[$i]);
  }
  // pack the db 1 out of 3 deletes
  if(rand(1, 3)==2) {
    $result = $db->query("VACUUM;");
  }
} elseif ($operation == "rename_node") {
  $query = "UPDATE folioge SET name='".$name."' WHERE id=".$id;
  $row=$result=$db->query($query);
} elseif ($operation == "move_node") {
  $query = "UPDATE folioge SET parent=".$parent." WHERE id=".$id;
  $row=$result=$db->query($query);
} elseif ($operation == "note_get") {
  $result=$db->query("SELECT note FROM folioge WHERE id=".$id);
  $row=$result->fetchObject();
  $note=$row->note;
  if(is_null($note)) {$note="";}
  echo stripslashes($note);
} elseif ($operation == "note_submit") {
  $query = "UPDATE folioge SET note='".$note."' WHERE ID=".$id;
  $row=$result=$db->query($query);
} elseif ($operation == "search_replace") {
  if (empty($replace)) {
    $result=$db->query("SELECT id, name FROM folioge WHERE note LIKE '%".$search."%' ORDER BY name");
    while($row=$result->fetchObject()){
      $id=$row->id;
      $name=$row->name;
      echo '<li><a href="#" onClick="$.jstree._focused().deselect_all(); $.jstree._focused().select_node($.jstree._focused()._get_node(\'#'.$id.'\'));">'.$name.'</a></li>';
    }   
  } else {
    $r1=$db->query("SELECT id, note FROM folioge WHERE note LIKE '%".$search."%' ORDER BY name");
    $t=0;
    while($row=$r1->fetchObject()){
      $id=$row->id;
      $note=$row->note;
      $query =  "UPDATE folioge SET note='".sqlite_escape_string(str_replace($search, $replace, $note))."' WHERE id=".$id;
      $r2=$db->query($query);
      $t = $t+1;
    }
    echo '<li>Replaced in '.$t.' records.</li>';   
  }
} elseif ($hack == "tree_xml") {
  $result=$db->query("SELECT * FROM folioge ORDER BY name");
  $id=0;
  $parent=0;
  $name="";
  header('Content-type: text/xml; charset=utf-8'); 
  echo '<?xml version="1.0" encoding="UTF-8"?>';
  echo '<root>';
  while($row=$result->fetchObject()){
    $id=$row->id;
    $parent=$row->parent;
    if(empty($parent)) {
      $parent="0";
    }
    $name=$row->name;
    echo '<item parent_id="'.$parent.'" id="'.$id.'"  ><content><name><![CDATA['.stripslashes($name).']]></name></content></item>\r\n';
  }   
  echo '</root>';
}

// recursive function crawls tree
function crawl($id,$db){ 
  global $s;

  if(!is_null($id)) {
    $result = $db->query("SELECT * FROM folioge where parent=".$id);
    $rows = $result->numRows();
    $s = $s . $id . ",";
    for ($i=0; $i<$rows; $i++) {
      $row=$result->fetchObject();
      $id=$row->id;
      crawl($id,$db);
    }
  } 
}
?>